<template>
  <el-form ref="loginFormRef" :model="loginForm" :rules="loginRules" size="large">
    <el-form-item prop="adminPhone">
      <el-input v-model="loginForm.adminPhone" placeholder="请输入手机号码" autocomplete="new-password">
        <template #prefix>
          <el-icon class="el-input__icon">
            <user />
          </el-icon>
        </template>
      </el-input>
    </el-form-item>
    <el-form-item prop="adminPassword">
      <el-input v-model="loginForm.adminPassword" type="password" placeholder="请输入密码" show-password>
        <template #prefix>
          <el-icon class="el-input__icon">
            <lock />
          </el-icon>
        </template>
      </el-input>
    </el-form-item>
  </el-form>
  <div class="login-btn">
    <el-button :icon="CircleClose" round size="large" @click="resetForm(loginFormRef)"> 重置 </el-button>
    <el-button :icon="UserFilled" round size="large" type="primary" :loading="loading" @click="login(loginFormRef)">
      登录
    </el-button>
  </div>
  <roleList ref="roleListRef" />
</template>
<script setup lang="ts">
import { ref, reactive } from "vue";
import { Login } from "@/api/interface/login";
import { authLogin } from '@/api/modules/login'
import { GlobalStore } from "@/stores";
import { CircleClose, UserFilled } from "@element-plus/icons-vue";
import { FormInstance } from "element-plus";
import roleList from './RoleList.vue'
const loginFormRef = ref<FormInstance>();
const loginForm = reactive<Login.ReqAuthLogin>({
  adminPhone: "",
  adminPassword: ""
});
const loginRules = reactive({
  adminPhone: [{ required: true, message: "请输入用户名", trigger: "blur" }],
  adminPassword: [{ required: true, message: "请输入密码", trigger: "blur" }]
});
const loading = ref(false);
const globalStore = GlobalStore();
// resetForm
const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.resetFields();
};
// login
const roleListRef = ref()
const login = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.validate(async valid => {
    if (!valid) return;
    const { data } = await authLogin(loginForm)
    globalStore.setToken(data.token);
    globalStore.setUserInfo(data.userInfo);
    globalStore.setSchoolList(data.schoolList);
    roleListRef.value.acceptParams(data.schoolList)
    loading.value = true;
    try {
    } finally {
      loading.value = false;
    }
  });
};
</script>